﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http.Results;
using System.Web.Mvc; // needed for lambda includes

using TheCup.ApiControllers;



namespace TheCup.Controllers
{
    public class CommentController : Controller
    {
        ApiControllers.CommentController apicc = new ApiControllers.CommentController();

        [Authorize(Roles = "Admin")]
        public ActionResult Index(CommentEntity? type)
        {
            return View(this.Get(type, null));
        }

        /// <summary>
        /// Get the first 100 comments
        /// </summary>
        /// <param name="type">Optional CommentEntity type</param>
        /// <returns>ToList</returns>
        public IEnumerable<CommentNew> Get(CommentEntity? type, int? take)
        {
            if (!take.HasValue)
            {
                take = 100;
            }

            using (var ctx = new TheCupContext())
            {
                if (type.HasValue)
                {
                    return ctx.CommentsNew.Where(x => x.EntityType == type).OrderByDescending(x => x.TimeStamp).Take(take.Value).ToList();
                }
                else
                {
                    return ctx.CommentsNew.OrderByDescending(x => x.TimeStamp).Take(take.Value).ToList();
                }
            }
        }

        public ActionResult Details(int id)
        {
            var actionResult = apicc.Details(id);
            var response = actionResult as OkNegotiatedContentResult<Comment>;
            if (response == null)
                return HttpNotFound();
            else
                return View(response.Content);
        }

        public CommentsViewModel Get(Guid uuid, int id, CommentEntity type)
        {
            CommentsViewModel cvm = new CommentsViewModel();
            cvm.Comments = apicc.Get(uuid);
            cvm.CommentType = type;
            cvm.Id = id;
            cvm.Uuid = uuid;
            return cvm;
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var comment = apicc.Details(id);
            return View(comment);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Create(int linkId, Guid linkUuid, string text, CommentEntity type)
        {
            TheCupContext context = new TheCupContext();

            Player player = context.Players.Where(p => p.Initials == User.Identity.Name).FirstOrDefault<Player>();
            Comment c = new Comment() { DateEntry = DateTime.Now, LinkUuid = linkUuid, LinkId = linkId, uuid = Guid.NewGuid(), Type = type, Text = text, Player = player, PlayerId = player.Id, OldUserGuId = player.uuid };

            context.Comments.Add(c);
            context.SaveChanges();

            return PartialView("_comment", c);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Delete(int id)
        {
            apicc.Delete(id);
            return Json("success");
        }
    }
}
